home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / muds / lpmud312.tar / lpmud312 / clilib.c < prev    next >
C/C++ Source or Header  |  1992-01-07  |  4KB  |  182 lines

  1. /*
  2.     Copyright (C) 1991, Marcus J. Ranum. All rights reserved.
  3. */
  4.  
  5. /*
  6. code to interface client MUDs with rwho server
  7.  
  8. this is a standalone library.
  9. */
  10.  
  11. #include    <stdio.h>
  12. #include    <ctype.h>
  13. #ifdef VMS
  14. #include        "tintop_dec:[amolitor.foo]types.h"
  15. #include        "tintop_dec:[amolitor.foo]socket.h"
  16. #include        "tintop_dec:[amolitor.foo]in.h"
  17. #include        "tintop_dec:[amolitor.foo]netdb.h"
  18. #else
  19. #include    <fcntl.h>
  20. #include    <sys/file.h>
  21. #include    <sys/time.h>
  22. #include    <sys/types.h>
  23. #include    <sys/socket.h>
  24. #include    <netinet/in.h>
  25. #include    <netdb.h>
  26. #include    <string.h>
  27. #endif /* VMS */
  28.  
  29. #include    "config.h"
  30. #ifdef MUDWHO /* Whole file depends on this */
  31. #include    "lint.h"
  32.  
  33. #define    DGRAMPORT        6888
  34.  
  35. #ifndef    NO_HUGE_RESOLVER_CODE
  36. extern    struct    hostent    *gethostbyname();
  37. #endif
  38.  
  39. extern    char    *malloc();
  40.  
  41.  
  42. static    int    dgramfd = -1;
  43. static    char    *password;
  44. static    char    *localnam;
  45. static    char    *lcomment;
  46. static    struct    sockaddr_in    addr;
  47. extern    int    current_time;
  48.  
  49. /* enable RWHO and send the server a "we are up" message */
  50. int rwhocli_setup(server,serverpw,myname,comment)
  51. char    *server;
  52. char    *serverpw;
  53. char    *myname;
  54. char    *comment;
  55. {
  56. #ifndef    NO_HUGE_RESOLVER_CODE
  57.     struct    hostent        *hp;
  58. #endif
  59.     char            pbuf[512];
  60.     char            *p;
  61.  
  62.     if(dgramfd != -1)
  63.         return(1);
  64.  
  65.     password = malloc(strlen(serverpw) + 1);
  66.     localnam = malloc(strlen(myname) + 1);
  67.     lcomment = malloc(strlen(comment) + 1);
  68.     if(password == (char *)0 || localnam == (char *)0 || lcomment == (char *)0)
  69.         return(1);
  70.     strcpy(password,serverpw);
  71.     strcpy(localnam,myname);
  72.     strcpy(lcomment,comment);
  73.  
  74.     p = server;
  75.     while(*p != '\0' && (*p == '.' || isdigit(*p)))
  76.         p++;
  77.  
  78.     if(*p != '\0') {
  79. #ifndef    NO_HUGE_RESOLVER_CODE
  80.         if((hp = gethostbyname(server)) == (struct hostent *)0)
  81.             return(1);
  82.         (void)bcopy(hp->h_addr,(char *)&addr.sin_addr,hp->h_length);
  83. #else
  84.         return(1);
  85. #endif
  86.     } else {
  87.         unsigned long    f;
  88.  
  89.         if((f = inet_addr(server)) == -1L)
  90.             return(1);
  91.         (void)bcopy((char *)&f,(char *)&addr.sin_addr,sizeof(f));
  92.     }
  93.  
  94.     addr.sin_port = htons(DGRAMPORT);
  95.     addr.sin_family = AF_INET;
  96.  
  97.     if((dgramfd = socket(AF_INET,SOCK_DGRAM,0)) < 0)
  98.         return(1);
  99.  
  100.     sprintf(pbuf,"U\t%.20s\t%.20s\t%.20s\t%.10d\t0\t%.25s",
  101.         localnam,password,localnam,current_time,comment);
  102.     sendto(dgramfd,pbuf,strlen(pbuf),0,&addr,sizeof(addr));
  103.     return(0);
  104. }
  105.  
  106.  
  107.  
  108.  
  109.  
  110. /* disable RWHO */
  111. int rwhocli_shutdown()
  112. {
  113.     char    pbuf[512];
  114.  
  115.     if(dgramfd != -1) {
  116.         sprintf(pbuf,"D\t%.20s\t%.20s\t%.20s",localnam,password,localnam);
  117.         sendto(dgramfd,pbuf,strlen(pbuf),0,&addr,sizeof(addr));
  118.         close(dgramfd);
  119.         dgramfd = -1;
  120.         free(password);
  121.         free(localnam);
  122.     }
  123.     return(0);
  124. }
  125.  
  126.  
  127.  
  128.  
  129.  
  130. /* send an update ping that we're alive */
  131. int rwhocli_pingalive()
  132. {
  133.     char    pbuf[512];
  134.  
  135.     if(dgramfd != -1) {
  136.         sprintf(pbuf,"M\t%.20s\t%.20s\t%.20s\t%.10d\t0\t%.25s",
  137.             localnam,password,localnam,current_time,lcomment);
  138.         sendto(dgramfd,pbuf,strlen(pbuf),0,&addr,sizeof(addr));
  139.     }
  140.     return(0);
  141. }
  142.  
  143.  
  144.  
  145.  
  146.  
  147. /* send a "so-and-so-logged in" message */
  148. int rwhocli_userlogin(uid,name,tim)
  149. char    *uid;
  150. char    *name;
  151. int    tim;
  152. {
  153.     char    pbuf[512];
  154.  
  155.     if(dgramfd != -1) {
  156.         sprintf(pbuf,"A\t%.20s\t%.20s\t%.20s\t%.20s\t%.10d\t0\t%.20s",
  157.             localnam,password,localnam,uid,tim,name);
  158.         sendto(dgramfd,pbuf,strlen(pbuf),0,&addr,sizeof(addr));
  159.     }
  160.     return(0);
  161. }
  162.  
  163.  
  164.  
  165.  
  166.  
  167. /* send a "so-and-so-logged out" message */
  168. int rwhocli_userlogout(uid)
  169. char    *uid;
  170. {
  171.     char    pbuf[512];
  172.  
  173.     if(dgramfd != -1) {
  174.         sprintf(pbuf,"Z\t%.20s\t%.20s\t%.20s\t%.20s",
  175.             localnam,password,localnam,uid);
  176.         sendto(dgramfd,pbuf,strlen(pbuf),0,&addr,sizeof(addr));
  177.     }
  178.     return(0);
  179. }
  180.  
  181. #endif /* MUDWHO whole file ! */
  182.